package cn.schoolwow.flow.monitor.module.business.flowRecord.aware;

import cn.schoolwow.ams.domain.block.common.operation.CustomerMessage;
import cn.schoolwow.ams.domain.block.common.operation.ListBlockHandlerBlockOperation;
import cn.schoolwow.ams.domain.block.list.clazz.ClazzListBlockConfigAware;
import cn.schoolwow.ams.domain.block.list.clazz.ClazzListBlockOption;
import cn.schoolwow.ams.domain.block.list.clazz.crud.ClazzListBlockListListener;
import cn.schoolwow.ams.domain.block.list.common.ListBlockOperationHandler;
import cn.schoolwow.ams.domain.block.list.common.ListBlockPagingResponse;
import cn.schoolwow.ams.module.block.listBlock.common.domain.operation.BlockOperationPosition;
import cn.schoolwow.ams.module.block.listBlock.common.domain.operation.ListBlockRequest;
import cn.schoolwow.flow.monitor.domain.FlowMonitorOption;
import cn.schoolwow.flow.monitor.module.business.flowRecord.domain.FlowRecord;
import cn.schoolwow.flow.monitor.module.business.flowRecord.domain.FlowRecordContext;
import cn.schoolwow.flow.monitor.module.business.flowRecord.flow.ExecuteFlowRecordFlow;
import cn.schoolwow.flow.monitor.module.business.flowRecord.flow.GetFlowRecordContextListFlow;
import cn.schoolwow.flow.monitor.module.common.domain.FlowMonitorConstant;
import cn.schoolwow.flow.monitor.module.common.flow.AddFlowCategoryNameCascadeRemoteSelectButton;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.util.domain.beanCopy.BeanCopy;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FlowRecordAware extends ClazzListBlockConfigAware {
    @Autowired
    private FlowMonitorOption flowMonitorOption;

    @Override
    public ClazzListBlockOption getAMSBlockConfig() {
        ClazzListBlockOption clazzEntityOption = new ClazzListBlockOption(
                FlowMonitorConstant.PROJECT_NAME, "流程记录", FlowRecord.class);
        clazzEntityOption.listBlockShowOption.requestFieldNames = new String[]{"name"};

        clazzEntityOption.clazzListBlockAPIListener.listListener = new ClazzListBlockListListener() {
            @Override
            public ListBlockPagingResponse list(JSONObject conditionMap) throws Exception {
                if(conditionMap.containsKey("categoryName")){
                    String categoryName = conditionMap.getString("categoryName");
                    categoryName = categoryName.replace(",","-");
                    conditionMap.put("categoryName", categoryName);
                }
                FlowContext flowContext = flowMonitorOption.flowRecordQuickFlow.executeFlow(new GetFlowRecordContextListFlow());
                List<FlowRecordContext> list = flowContext.getData("list", List.class);
                List<FlowRecord> flowRecordList = BeanCopy.cloneObjectList(list, FlowRecord.class);
                if(conditionMap.containsKey("name")){
                    String name = conditionMap.getString("name");
                    flowRecordList.removeIf(flowRecord -> !flowRecord.name.contains(name));
                }
                if(conditionMap.containsKey("categoryName")){
                    String categoryName = conditionMap.getString("categoryName");
                    flowRecordList.removeIf(flowRecord -> !flowRecord.categoryName.contains(categoryName));
                }
                return new ListBlockPagingResponse(flowRecordList);
            }
        };

        {
            ListBlockHandlerBlockOperation blockOperation = new ListBlockHandlerBlockOperation("执行记录", new ListBlockOperationHandler() {
                @Override
                public CustomerMessage handle(ListBlockRequest listBlockRequest) throws Exception {
                    flowMonitorOption.flowRecordQuickFlow.startFlow(new ExecuteFlowRecordFlow())
                            .putInstanceData(listBlockRequest)
                            .execute();
                    return null;
                }
            });
            clazzEntityOption.blockOperationList.add(blockOperation);
        }
        {
            ListBlockHandlerBlockOperation blockOperation = new ListBlockHandlerBlockOperation("批量执行记录", new ListBlockOperationHandler() {
                @Override
                public CustomerMessage handle(ListBlockRequest listBlockRequest) throws Exception {
                    flowMonitorOption.flowRecordQuickFlow.startFlow(new ExecuteFlowRecordFlow())
                            .putInstanceData(listBlockRequest)
                            .execute();
                    return null;
                }
            });
            blockOperation.position = BlockOperationPosition.MultipleSelect;
            clazzEntityOption.blockOperationList.add(blockOperation);
        }
        {
            flowMonitorOption.flowRecordQuickFlow.startFlow(new GetFlowRecordContextListFlow())
                    .next(new AddFlowCategoryNameCascadeRemoteSelectButton())
                    .putInstanceData(clazzEntityOption)
                    .execute();
        }
        return clazzEntityOption;
    }

}
